package erwiki.ui.app.parts;

import org.eclipse.rap.fileupload.DiskFileUploadReceiver;
import org.eclipse.rap.fileupload.FileUploadEvent;
import org.eclipse.rap.fileupload.FileUploadHandler;
import org.eclipse.rap.fileupload.FileUploadListener;
import org.eclipse.rap.rwt.widgets.FileUpload;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import erwiki.api.core.Engine;
import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;

public class CreateWikiPage {

	public static final String ID = "org.elwiki.e4.ui.app.part.createWikiPage"; //$NON-NLS-1$

	private Text textName;
	private Text textContent;

	final protected FileUploadHandler uploadHandler;

	// == CODE ================================================================

	@Inject
	public CreateWikiPage() {
		final DiskFileUploadReceiver receiver = new DiskFileUploadReceiver();
		this.uploadHandler = new FileUploadHandler(receiver);
		this.uploadHandler.addUploadListener(new FileUploadListener() {
			@Override
			public void uploadProgress(FileUploadEvent event) {
			}

			@Override
			public void uploadFailed(FileUploadEvent event) {
			}

			@Override
			public void uploadFinished(FileUploadEvent event) {
				String filePath = receiver.getTargetFiles()[0].getAbsolutePath();
				// System.out.println("Stored file: " + filePath);

				// :FVK: this.m_engine.getAttachmentManager();
				//:FVK: -- old code
				//IAttachmentManager attMgr = AppUiActivator.getDefault().getAppSession().getAttachmentManager();
				//attMgr.storeAttachment(filePath);
			}
		});
	}

	/**
	 * Create contents of the view part.
	 *
	 * @param parent
	 */
	@PostConstruct
	public void postConstruct(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(2, false));
		new Label(container, SWT.NONE);
		{
			Composite composite = new Composite(container, SWT.NONE);
			composite.setLayout(new GridLayout(3, false));
			composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
			{
				Button btnCreatePage = new Button(composite, SWT.NONE);
				btnCreatePage.addMouseListener(new MouseAdapter() {
					private static final long serialVersionUID = -5392919654268287963L;

					@Override
					public void mouseUp(MouseEvent e) {
						createPage();
					}
				});
				btnCreatePage.setText("Create");
			}
			new Label(composite, SWT.NONE);
			{
				Button btnNewButton = new Button(composite, SWT.NONE);
				btnNewButton.setText("Create Attach");
			}
			{
				final FileUpload fileUpload;
				fileUpload = new FileUpload(composite, SWT.NONE);
				fileUpload.setText("Select File");
				fileUpload.addSelectionListener(new SelectionAdapter() {
					private static final long serialVersionUID = -8410319872694940683L;

					@Override
					public void widgetSelected(SelectionEvent e) {
						fileUpload.submit(CreateWikiPage.this.uploadHandler.getUploadUrl());
					}
				});
			}
		}
		{
			Label lblName = new Label(container, SWT.NONE);
			lblName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			lblName.setText("Name:");
		}
		{
			this.textName = new Text(container, SWT.BORDER);
			GridData gd_textName = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
			gd_textName.widthHint = 227;
			this.textName.setLayoutData(gd_textName);
		}
		{
			Label lblId = new Label(container, SWT.NONE);
			lblId.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			lblId.setText("id:");
		}
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		{
			this.textContent = new Text(container, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
			this.textContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		}

		/*:FVK: -- old code
		createActions();
		initializeToolBar();
		initializeMenu();*/
	}

	protected void createPage() {
		//TODO: создать страницу.
		/*
		Engine engine = AppUiActivator.getDefault().getAppSession().getWikiEngine();
		engine.createPage(this.textName.getText(), this.textContent.getText());
		*/
	}

}
